package edu.faytechcc.csc.gui;

import edu.faytechcc.csc.business.Model;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Represents the portion of the main GUI that retrieve information from the 
 * user about the students to be used in the batch process.
 * 
 * @author Charles Bryan
 */
public class StudentPanel extends JPanel
{

    private JRadioButton singleButton;
    private JRadioButton multipleButton;
    private ButtonGroup buttonGroup;
    private JLabel sectionLabel;
    private JTextField sectionTextField;
    private JLabel studentIDLabel;
    private JTextField studentIdTextField;
    private FileChooserPanel fcPanel;
    private JPanel innerPanel;

    public StudentPanel()
    {
        innerPanel = new JPanel();
        innerPanel.setLayout(new GridLayout(5, 1));
        innerPanel.setBorder(
                BorderFactory.createTitledBorder("Student Information"));
        this.add(innerPanel);
        buildPanel();
    }

    /**
     * Returns an empty string and sets the correct field in the Model, model
     * if the user has entered correct data, otherwise, this method returns 
     * a String with an error message in it.
     * 
     * @param model The Model to set the fields in
     * @return Empty string if all goes well, an error message as a String if 
     * not.
     */
    public String getText(Model model)
    {
        String result = "";
        if (this.sectionTextField.getText().isEmpty())
        {
            result += "Section feild is empty.\n";
            sectionTextField.setBackground(Color.PINK);
        }
        else
        {
            model.setSection(this.sectionTextField.getText());
        }
        if (singleButton.isSelected())
        {
            if (this.studentIdTextField.getText().isEmpty())
            {
                result += "Student ID feild is empty.\n";
                studentIdTextField.setBackground(Color.PINK);
            }
            else
            {
                try
                {
                    model.setStudent(this.studentIdTextField.getText());
                } catch (Exception e)
                {
                    result += "Student ID feild is incorrectly formatted.\n";
                    studentIdTextField.setBackground(Color.PINK);
                }
            }
        }
        else
        {
            if (this.fcPanel.getText().isEmpty())
            {
                result += "Multiple Students - File feild is empty.\n";
                this.fcPanel.setWarningColor(true);
            }
            else
            {
                try
                {
                    model.setStudentFile(this.fcPanel.getText());
                } catch (Exception e)
                {
                    result += "Multiple Student file is incorrectly formatted.\n";
                    this.fcPanel.setWarningColor(true);
                }
            }
        }
        return result;
    }

    /**
     * Sets up the components for the GUI
     */
    private void buildPanel()
    {
        JPanel tempPanel = new JPanel();
        tempPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        sectionLabel = new JLabel("Enter the Section:");
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0.0;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0, 5, 0, 4);
        tempPanel.add(this.sectionLabel, c);

        sectionTextField = new JTextField(1);
        sectionTextField.addFocusListener(new TextFieldFocusListener());
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(0, 0, 0, 4);
        c.weightx = 0.1;
        c.gridx = 1;
        c.gridy = 0;
        tempPanel.add(this.sectionTextField, c);

        innerPanel.add(tempPanel);

        buttonGroup = new ButtonGroup();

        this.singleButton = new JRadioButton("Single Student", true);
        this.singleButton.addActionListener(new RadioButtonActionListner());
        this.singleButton.setToolTipText("Select if you want to process a "
                + " single student.");
        buttonGroup.add(singleButton);

        innerPanel.add(this.singleButton);

        tempPanel = new JPanel();
        tempPanel.setLayout(new GridBagLayout());
        tempPanel.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
        c = new GridBagConstraints();

        this.studentIDLabel = new JLabel("Student ID:");
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0.0;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0, 0, 0, 4);
        tempPanel.add(this.studentIDLabel, c);


        this.studentIdTextField = new JTextField(1);
        studentIdTextField.addFocusListener(new TextFieldFocusListener());
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.1;
        c.gridx = 1;
        c.gridy = 0;
        tempPanel.add(this.studentIdTextField, c);

        innerPanel.add(tempPanel);

        this.multipleButton = new JRadioButton("Multiple Students");
        this.multipleButton.addActionListener(new RadioButtonActionListner());
        this.multipleButton.setToolTipText("Select if you have a file with"
                + " a list of student names. "
                + "You may create one using the Create Menu bar.");
        buttonGroup.add(multipleButton);
        innerPanel.add(this.multipleButton);

        FileNameExtensionFilter filter =
                new FileNameExtensionFilter("Text Files: *.txt *.dat", "txt", "dat");
        fcPanel = new FileChooserPanel("File:", 40, filter);
        fcPanel.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
        fcPanel.setEnabled(false);
        innerPanel.add(fcPanel);

    }

    private class RadioButtonActionListner implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (singleButton.isSelected())
            {
                fcPanel.setEnabled(false);
                studentIDLabel.setEnabled(true);
                studentIdTextField.setEnabled(true);
            }
            else
            {
                fcPanel.setEnabled(true);
                studentIDLabel.setEnabled(false);
                studentIdTextField.setEnabled(false);
            }
        }
    }

    private class TextFieldFocusListener implements FocusListener
    {

        @Override
        public void focusGained(FocusEvent e)
        {
        }

        //If the text field is pink from a warning, turn the color white when
        //text is entered.
        @Override
        public void focusLost(FocusEvent e)
        {
            if (e.getSource() == sectionTextField)
            {
                if (sectionTextField.getBackground() == Color.PINK
                        && !sectionTextField.getText().isEmpty())
                {
                    sectionTextField.setBackground(Color.WHITE);
                }
            }
            else if (e.getSource() == studentIdTextField)
            {
                if (studentIdTextField.getBackground() == Color.PINK
                        && !studentIdTextField.getText().isEmpty())
                {
                    studentIdTextField.setBackground(Color.WHITE);
                }
            }
        }
    }
}
